﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Thaire.Investment.Common;
using ThaireInvestmentWeb.Util;
using ThaireInvestmentWeb.WcfSerivce;

namespace ThaireInvestmentWeb.Models
{
    public class FixedIncomeSaleModel
    {
        public FixIncomeSecuritiesTransactionScreenData ScreenData { set; get; }
        public BindingList<SelectListItem> CounterPartyList { set; get; }
        public BindingList<SelectListItem> TypeOfPayment { set; get; }


        [Required(ErrorMessage = "โปรดระบุ")]
        public String SelectedCounterParty { set; get; }
        [Required(ErrorMessage = "โปรดระบุ")]
        public String SelectedTypeOfPayment { set; get; }

        public String TransactionType { set; get; }

        [Required(ErrorMessage = "โปรดระบุ")]
        [Range(0, (double)decimal.MaxValue, ErrorMessage = "ข้อมูลต้อง >= 0")]
        public Decimal? Yield { set; get; }

        [Required(ErrorMessage = "โปรดระบุ")]
        [DataType(DataType.Date)]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
        [Display(Name = "Trade Date")]
        public DateTime? TradeDate { set; get; }

        [Required(ErrorMessage = "โปรดระบุ")]
        [DataType(DataType.Date)]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
        [Display(Name = "Settlement Date")]
        public DateTime? SettlementDate { set; get; }

        [Required(ErrorMessage = "โปรดระบุ")]
        [Range(1, (double)decimal.MaxValue, ErrorMessage = "ข้อมูลต้อง >= 1")]
        public int? Unit { set; get; }

        [Required(ErrorMessage = "โปรดระบุ")]
        [Range(0, (double)decimal.MaxValue, ErrorMessage = "ข้อมูลต้อง >= 0")]
        public Decimal? CleanPrice { set; get; }

        [Required(ErrorMessage = "โปรดระบุ")]
        public String RefNo { set; get; }

        //public String AI { set; get; }
        //public String GrossPrice { set; get; }

        [Range(0, (double)decimal.MaxValue, ErrorMessage = "ข้อมูลต้อง >= 0")]
        public Decimal? AI { set; get; }


        public Decimal? GrossPrice { set; get; }

        public String BtnSave { set; get; }
        public String BtnCancel { set; get; }


        public AddFixedIncomePurchaseModel PurchaseModel { set; get; }

        public FixedIncomeSaleModel()
        {
            CounterPartyList = new BindingList<SelectListItem>();
            TypeOfPayment = new BindingList<SelectListItem>();
            TransactionType = "Sale";
        }

        public static FixedIncomeSaleModel Build(FixIncomeSecuritiesTransactionScreenData data)
        {
            FixedIncomeSaleModel model = new FixedIncomeSaleModel();
            model.ScreenData = data;
            model.CounterPartyList.Add(WebUtil.SelectListSelectDefault());
            model.TypeOfPayment.Add(WebUtil.SelectListSelectDefault());
            model.PurchaseModel = new AddFixedIncomePurchaseModel(data.FixincomesecuritiesPurchaseDTO.Header);
            model.PurchaseModel.Purchase = data.FixincomesecuritiesPurchaseDTO.Purhcase;
            foreach (var item in data.CounterParties)
            {
                model.CounterPartyList.Add(new SelectListItem() { Text = item.CounterpartiesCodeName, Value = item.CounterpartiesId.ToString() });
            }
            foreach (var item in data.TypeOfPayment)
            {
                model.TypeOfPayment.Add(new SelectListItem() { Text = item.PaymentTypeName, Value = item.PaymentTypeId.ToString() });
            }



            model.PurcheseID = model.PurchaseModel.Purchase.FixincomesecuritiesPurchaseId.ToString();
            model.HeaderID = model.PurchaseModel.HeaderID.ToString(); ;


            if (data.FixIncomeSecuritiesTransactionDTO != null && data.FixIncomeSecuritiesTransactionDTO.FixIncomeSecuritiesTransaction !=null)
            {
                model.SetFixIncomeSecuritiesTransaction(data.FixIncomeSecuritiesTransactionDTO.FixIncomeSecuritiesTransaction);
            }

            return model;

        }
        public void SetFixIncomeSecuritiesTransaction(FixIncomeSecuritiesTransaction tran)
        {
            if (tran.Cleanprice.HasValue)
                 CleanPrice = tran.Cleanprice.Value;
               // CleanPrice = tran.Cleanprice.Value.ToString("#0.0000");

            HeaderID = tran.FixincomesecuritiesHeaderID.ToString();
            if (tran.GrossPrice.HasValue)
                //GrossPrice = tran.GrossPrice.Value.ToString("#0.0000");
                GrossPrice = tran.GrossPrice;
            if (tran.AI.HasValue)
                AI = tran.AI;

            PurcheseID = tran.PurchaseID.ToString(); ;
            RefNo = tran.RefNO;
            TransactionID = tran.TransactionID.ToString(); ;
            TransactionType = tran.TransactionType;
            if (tran.TypeofpaymentID.HasValue)
                SelectedTypeOfPayment = tran.TypeofpaymentID.Value.ToString();

            if (tran.CounterpartyID.HasValue)
                SelectedCounterParty = tran.CounterpartyID.Value.ToString();

            if (tran.Unit.HasValue)
                Unit = tran.Unit.Value;
            if (tran.Yield.HasValue)
                Yield = tran.Yield.Value;//.ToString("#0.0000");

            if (tran.SettlementDate.HasValue)
                SettlementDate = tran.SettlementDate.Value;

            if (tran.TradeDate.HasValue)
                TradeDate = tran.TradeDate.Value;


            
        }
        public FixIncomeSecuritiesTransaction ToFixIncomeSecuritiesTransaction()
        {
            FixIncomeSecuritiesTransaction tran = new FixIncomeSecuritiesTransaction();
            tran.Cleanprice = CleanPrice;// DataConverter.ConvertStringToDecimalNull(CleanPrice);
            tran.CounterpartyID = DataConverter.ConvertStringToIntNull(SelectedCounterParty);
            tran.FixincomesecuritiesHeaderID = DataConverter.ConvertStringToIntNull(HeaderID).GetValueOrDefault();
            //tran.GrossPrice = WebUtil.ConvertStringToDecimalNull(GrossPrice);
            tran.GrossPrice = GrossPrice;
            tran.AI = AI;

            tran.PurchaseID = DataConverter.ConvertStringToIntNull(PurcheseID).GetValueOrDefault();
            tran.RefNO = RefNo;
            tran.TransactionID = DataConverter.ConvertStringToIntNull(TransactionID).GetValueOrDefault();
            tran.TransactionType = TransactionType;
            tran.TypeofpaymentID = DataConverter.ConvertStringToIntNull(SelectedTypeOfPayment);
            tran.Unit = Unit;//  DataConverter.ConvertStringToIntNull(Unit);
            tran.Yield = Yield;// DataConverter.ConvertStringToDecimalNull(Yield);
            tran.TradeDate = TradeDate;
            tran.SettlementDate = SettlementDate;

            return tran;
        }

        public bool UpdateMode { get; set; }
        public String TransactionID { get; set; }
        public String PurcheseID { get; set; }
        public String HeaderID { get; set; }
    }
}